﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstController : MonoBehaviour, ISceneController, IUserAction {

	public CCActionManager actionManager { get; set;}
	public GameObject move1,move2;
	public bool isRunning;
	public ShoreController leftController, rightController;
	public River river;
	public BoatController boatController;
    public RoleController[] roleControllers;

	public void JudgeCallback(bool isRuning, string message)
    {
        this.gameObject.GetComponent<UserGUI>().gameMessage = message;
        this.isRunning = isRunning;
    }

	// the first scripts
	void Awake () {
		SSDirector.GetInstance().currentSceneController = this;
        LoadResources();
        this.gameObject.AddComponent<UserGUI>();
        this.gameObject.AddComponent<CCActionManager>();
        this.gameObject.AddComponent<JudgeController>();
	}
	 
	// loading resources for first scence
	public void LoadResources () {
		//role
        roleControllers = new RoleController[6];
        for (int i = 0; i < 6; ++i) {
            roleControllers[i] = new RoleController();
            roleControllers[i].CreateRole(Position.role_shore_left[i], i < 3 ? true : false, i);
        }

        //shore
        leftController = new ShoreController();
        leftController.CreateShore(Position.left);
        leftController.GetShore().shore.name = "left_shore";
        rightController = new ShoreController();
        rightController.CreateShore(Position.right);
        rightController.GetShore().shore.name = "right_shore";

        //将人物添加并定位至左岸  
        foreach (RoleController roleController in roleControllers)
        {
            roleController.GetRoleModel().role.transform.localPosition = leftController.AddRole(roleController.GetRoleModel());
        }
        //boat
        boatController = new BoatController();
        boatController.CreateBoat(Position.left_boat);

        //river
        river = new River(Position.river);

        //move
        //moveController = new MoveCtrl();

        isRunning = true;
	}

	public void MoveBoat()
	{
		if (isRunning == false || actionManager.IsMoving()) return;
        
        Vector3 destination = boatController.GetBoatModel().isRight ? Position.left_boat : Position.right_boat;
        actionManager.MoveBoat(boatController.GetBoatModel().boat, destination, 5);
        if (boatController.GetBoatModel().isRight)
        {
            if(boatController.GetBoatModel().roles[0] != null && boatController.GetBoatModel().roles[1] == null)
            {
                Vector3 tmp = Position.role_boat_left[0];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[0].role, tmp, 5);
            }
            else if(boatController.GetBoatModel().roles[0] == null && boatController.GetBoatModel().roles[1] != null)
            {
                Vector3 tmp = Position.role_boat_left[1];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[1].role, tmp, 5);
            }
            else if(boatController.GetBoatModel().roles[0] != null && boatController.GetBoatModel().roles[1] != null)
            {
                Vector3 tmp1 = Position.role_boat_left[0];
                Vector3 tmp2 = Position.role_boat_left[1];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[0].role, tmp1, 5);
                actionManager.MoveBoat(boatController.GetBoatModel().roles[1].role, tmp2, 5);
            }
        }
        else 
        {
            if(boatController.GetBoatModel().roles[0] != null && boatController.GetBoatModel().roles[1] == null)
            {
                Vector3 tmp = Position.role_boat_right[0];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[0].role, tmp, 5);
            }
            else if(boatController.GetBoatModel().roles[0] == null && boatController.GetBoatModel().roles[1] != null)
            {
                Vector3 tmp = Position.role_boat_right[1];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[1].role, tmp, 5);
            }
            else if(boatController.GetBoatModel().roles[0] != null && boatController.GetBoatModel().roles[1] != null)
            {
                Vector3 tmp1 = Position.role_boat_right[0];
                Vector3 tmp2 = Position.role_boat_right[1];
                actionManager.MoveBoat(boatController.GetBoatModel().roles[0].role, tmp1, 5);
                actionManager.MoveBoat(boatController.GetBoatModel().roles[1].role, tmp2, 5);
            }
        }
        
        boatController.GetBoatModel().isRight = !boatController.GetBoatModel().isRight;
	}

	public void MoveRole(Role roleModel)
	{
		if (isRunning == false || actionManager.IsMoving()) return;
        Vector3 destination, mid_destination;
        if (roleModel.inBoat)
        {
            
            if (boatController.GetBoatModel().isRight)
                destination = rightController.AddRole(roleModel);
            else
                destination = leftController.AddRole(roleModel);
            if (roleModel.role.transform.localPosition.y > destination.y)
                mid_destination = new Vector3(destination.x, roleModel.role.transform.localPosition.y, destination.z);
            else
                mid_destination = new Vector3(roleModel.role.transform.localPosition.x, destination.y, destination.z);
             
            // 局部坐标转世界坐标
            // Transform transform = roleModel.role.transform;
            // Vector3 world_des = transform.TransformPoint(destination);
            // Vector3 world_mid = transform.TransformPoint(mid_destination);
            actionManager.MoveRole(roleModel.role, mid_destination, destination, 5);
            roleModel.onRight = boatController.GetBoatModel().isRight;
            boatController.RemoveRole(roleModel);
        }
        else
        {
            
            if (boatController.GetBoatModel().isRight == roleModel.onRight)
            {
                if (roleModel.onRight)
                {
                    rightController.RemoveRole(roleModel);
                }
                else
                {
                    leftController.RemoveRole(roleModel);
                }
                destination = boatController.AddRole(roleModel);
                if (roleModel.role.transform.localPosition.y > destination.y)
                    mid_destination = new Vector3(destination.x, roleModel.role.transform.localPosition.y, destination.z);
                else
                    mid_destination = new Vector3(roleModel.role.transform.localPosition.x, destination.y, destination.z);
                // Transform transform = roleModel.role.transform;
                // Vector3 world_des = transform.TransformPoint(destination);
                // Vector3 world_mid = transform.TransformPoint(mid_destination);
                actionManager.MoveRole(roleModel.role, mid_destination, destination, 5);
            }
        }
	}

	public void Check()
	{
		
	}

	public void Pause ()
	{
		throw new System.NotImplementedException ();
	}

	public void Resume ()
	{
		throw new System.NotImplementedException ();
	}


	// Use this for initialization
	void Start () {
		//give advice first
	}
	

}
